<html>
<head><meta charset="utf-8"><title>how to check if caller impls conditional trait bound · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20check.20if.20caller.20impls.20conditional.20trait.20bound.html">how to check if caller impls conditional trait bound</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240935344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20to%20check%20if%20caller%20impls%20conditional%20trait%20bound/near/240935344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Hughes <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20to.20check.20if.20caller.20impls.20conditional.20trait.20bound.html#240935344">(Jun 01 2021 at 12:44)</a>:</h4>
<p>Hi, I have a type <code>T&lt;U&gt;</code> which has the following conditional trait impl:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">U</span>: <span class="nc">SomeTrait</span><span class="o">&gt;</span><span class="w"> </span><span class="n">SomeTrait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>I've created a little intrinsic that should return <code>true</code> (a bit like how <code>needs_drop</code> works) if a certain instance of <code>T</code> implements <code>SomeTrait</code>.  Unfortunately, it doesn't seem to work when I have these conditional trait implementations. For example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">SomeTrait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="n">core</span>::<span class="n">intrinsics</span>::<span class="n">has_some_trait</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;&gt;</span><span class="p">()</span><span class="w"> </span><span class="c1">// returns false :(</span>
</code></pre></div>
<p>I think what's happening here is when the <code>has_some_trait</code> method eventually calls the <code>type_known_to_meet_bounds_modulo_regions</code>, it doesn't know about the concrete <code>T&lt;usize&gt;</code>, and instead is generic over <code>T</code>.</p>
<p>I _think_ I need to monomorphise this? So inside my intrinsic I call <code>Instance::mono(tcx, def_id)</code> but this ICE's with <code>Instance::mono has type parameters</code>. Is this because monomorphisation can only happen at codegen or something? </p>
<p>This stuff is pretty new to me, so I'd appreciate any help. Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>